package com.woniuxy.controller;

import com.woniuxy.domain.TreeNode;
import com.woniuxy.pojo.Permission;
import com.woniuxy.pojo.User;
import com.woniuxy.service.PermissionService;
import com.woniuxy.util.TreeNodeBuilder;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
@Controller
public class IndexController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private PermissionService permissionService;
    @RequestMapping(value = "/index.do")
    public String index(){
        return "index";
    }
    @RequestMapping(value = "/loadIndexLeftMenu.do")
    @ResponseBody
    public List<TreeNode> loadIndexLeftMenu(){
        logger.debug("*********loadIndexLeftMenu**********");
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        User user=(User)session.getAttribute("USER_SESSION");
        List<Permission> permissionList=permissionService.selectMenusByUid(user.getId());
        List<TreeNode> treeNodes=new ArrayList<>();
        for (Permission permission:permissionList) {
            int id=permission.getId();
            int pid=permission.getPid();
            String title=permission.getName();
            String icon=permission.getIcon();
            String href=permission.getHref();
            boolean spread=permission.getOpen()==1?true:false;
            TreeNode treeNode=new TreeNode(id,pid,title,icon,href,spread);
            treeNodes.add(treeNode);
        }
        //构造层级关系
        List<TreeNode> treeNodes1= TreeNodeBuilder.build(treeNodes,1);
        return treeNodes1;
    }
    @RequestMapping(value = "toUserManager.do")
    public String toUserManager(){
        return "user/userList";
    }
    @RequestMapping(value = "toUserPage.do")
    public String toUserPage(){
        return "user/userAdd";
    }
    @RequestMapping(value = "/toAssignRole.do")
    public String toAssignRole(){
        return "/user/assignRole";
    }
    @RequestMapping(value = "toRoleManager.do")
    public String toRoleManager(){
        return "role/roleList";
    }
    @RequestMapping(value = "/toAssignRight.do")
    public String toAssignRight(){
        return "/role/assignRight";
    }
}
